package com.firebird.app.fekpsl.ui.activity.me

import android.text.InputFilter
import android.text.InputType
import android.view.View
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.mvp.me.BindEmailContract
import com.firebird.app.fekpsl.mvp.me.BindEmailPresenter
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.me_bind_email.*
import kotlinx.android.synthetic.main.toolbar.*

class BindEmailActivity : BaseMvpActivity<BindEmailContract.View, BindEmailContract.Presenter>(), BindEmailContract.View {
    private var pf_bind_email:String by Preference(Constant.EMAIL_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")

    override fun createPresenter(): BindEmailContract.Presenter = BindEmailPresenter()

    private val mDialog by lazy {
        // DialogUtil.getWaitDialog(this, getString(R.string.save_ing))
    }

    override fun showLoading() {
        // mDialog.show()
    }

    override fun hideLoading() {
        // mDialog.dismiss()
    }

    override fun bindEmailSuccess(success: Boolean) {
        if(success){
            pf_bind_email=et_me_bind_email.text.toString()
            finish()
        }else{
            // showToast(getString(R.string.me_save_fail))
        }
    }
    override fun attachLayoutRes(): Int = R.layout.me_bind_email

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.me_bind_email)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        et_me_bind_email.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)
        et_me_bind_email.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(11))) //限制输入长度为11
        et_me_bind_email.setText(pf_bind_email)
        btn_save.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_save -> {
                bindEmail()
            }
        }
    }
    private fun bindEmail() {
        if (validate()) {
            mPresenter?.bindEmail(pf_uuid, et_me_bind_email.text.toString()
            )
        }
    }
    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val bindEmail: String = et_me_bind_email.text.toString()

        if (bindEmail.isEmpty()) {
            et_me_bind_email.error = getString(R.string.me_bind_email_not_empty)
            valid = false
        }
        if (bindEmail.length>40){
            et_me_bind_email.error = getString(R.string.me_bind_email_len_error)
            valid = false
        }
        return valid
    }

}